using Infrastructure;
using Infrastructure.Attribute;
using Infrastructure.Enums;
using Infrastructure.Model;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using ZR.Model.Dto;
using ZR.Model.Business;
using ZR.Service.Business.IBusinessService;
using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Filters;
using ZR.Common;
using ZR.Common.Cache;
using Infrastructure.Extensions;
using Newtonsoft.Json;
using ZR.Model.System;
using ZR.Service;
using Aliyun.OSS;

//创建时间：2023-12-21
namespace ZR.Admin.WebApi.Controllers
{
    /// <summary>
    /// 学生表
    /// </summary>
    [Verify]
    [Route("business/AStudent")]
    public class AStudentController : BaseController
    {
        /// <summary>
        /// 学生表接口
        /// </summary>
        //[Autowired]
        public IAStudentService? _AStudentService { get; set; }


        public AStudentController(IAStudentService AStudentService)
        {
            _AStudentService = AStudentService;
        }

        /// <summary>
        /// 查询学生表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "business:astudent:list")]
        public IActionResult QueryAStudent([FromQuery] AStudentQueryDto parm)
        {
            var response = _AStudentService.GetList(parm);
            //test.show();
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询学生表详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "business:astudent:query")]
        public IActionResult GetAStudent(long Id)
        {
            AStudent info = null;
            //读取redis里的数据，有则直接返回，没有再读取数据库，并保存到redis
            string json = RedisServer.Student.Get($"student_{Id}");
            if (json.IsNotEmpty())
            {
                info = (AStudent)JsonConvert.DeserializeObject(json, typeof(AStudent));
            }
            else
            {
                var response = _AStudentService.GetFirst(x => x.Id == Id);
                info = response.Adapt<AStudent>();
                RedisServer.Student.Set($"student_{Id}", info);
            }
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加学生表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "business:astudent:add")]
        [Log(Title = "学生表", BusinessType = BusinessType.INSERT)]
        public IActionResult AddAStudent([FromBody] AStudent parm)
        {
            //var modal = parm.Adapt<AStudent>().ToCreate(HttpContext);

            var response = _AStudentService.AddAStudent(parm);
            return ToResponse(response);
        }

        /// <summary>
        /// 更新学生表
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "business:astudent:edit")]
        [Log(Title = "学生表", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateAStudent([FromBody] AStudent parm)
        {
            //var modal = parm.Adapt<AStudent>().ToUpdate(HttpContext);
            var response = _AStudentService.UpdateAStudent(parm);
            RedisServer.Student.Del($"student_{parm.Id}");
            return ToResponse(response);
        }

        /// <summary>
        /// 删除学生表
        /// </summary>
        /// <returns></returns>
        [HttpDelete("{ids}")]
        [ActionPermissionFilter(Permission = "business:astudent:delete")]
        [Log(Title = "学生表", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteAStudent(string ids)
        {
            int[] idsArr = Tools.SpitIntArrary(ids);
            if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); }
            var response = _AStudentService.Delete(idsArr);
            foreach (int id in idsArr) {
                RedisServer.Student.Del($"student_{id}");
            }
            return ToResponse(response);
        }



    }
}